package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.net.handler.GetGroupMemberHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetGroupMemberReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.GroupMemberHeadersAdapter;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.Iterator;
import java.util.List;

/**
 * Created by yaohuix on 2016/5/9  16:43
 * <p/>
 * 说明：群成员 界面
 */
public class GroupMemberActivity extends BaseActivity {

    private RecyclerView mRecyclerView;
    private StickyRecyclerHeadersDecoration mHeadersDecoration;
    private GroupMemberHeadersAdapter mAdapter;
    private String mGroupId;//群ID
    private String mGroupCode;//环信群id
    private String mGroupMasterId;//群主ID

    @Override
    public void initIntentData(Intent intent) {
        mGroupId = intent.getStringExtra(DataKey.INDEX);
        mGroupCode = intent.getStringExtra(DataKey.GROUP_CODE);
        mGroupMasterId = intent.getStringExtra(DataKey.USER_ID);
    }

    @Override
    public void initDatas() {
        getGroupMember(mGroupId);
    }

    @Override
    public void initTop() {
        enableTitle(true, "群成员");
        enableRefresh(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_common);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new GroupMemberHeadersAdapter(mContext, null));

        // Add the sticky headers decoration
        mHeadersDecoration = new StickyRecyclerHeadersDecoration(mAdapter);
        mRecyclerView.addItemDecoration(mHeadersDecoration);
        mAdapter.setGroupId(mGroupId);
        mAdapter.setGroupMasterId(mGroupMasterId);
        //注册一个适配器数据观察者，当数据发生变化时，刷新
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                mHeadersDecoration.invalidateHeaders();
            }
        });
    }

    @Override
    public void initListeners() {

    }


    /**
     * 获取群成员
     *
     * @param groupId 群的id（不是环信groupId）
     * @return
     * @author yaohuix
     * @date 2016/5/9 15:52
     */
    public void getGroupMember(String groupId) {
        GetGroupMemberReq getGroupMemberReq = new GetGroupMemberReq(groupId);
        HttpLoader.getInstance().sendRequest(new GetGroupMemberHandler(getGroupMemberReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<UserModel> list = (List<UserModel>) model;
                Iterator<UserModel> iterator = list.iterator();

                UserModel masterUserModel = null; //群主
                while (iterator.hasNext()) {
                    UserModel userModel = iterator.next();
                    if ((userModel.getUser_id() + "").equals(mGroupMasterId)) {//如果是群主的ID
                        masterUserModel = userModel; //保存群主引用
                        iterator.remove();//从集合中删除群主
                    }
                }
                if (masterUserModel != null) {
                    list.add(0, masterUserModel);//插到第一个
                }
                mAdapter.update(list);
            }
        }, 0));
    }
}
